//Townsend.cpp
//
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;
			
//Stats
		//Hero
		string 	hname;
		int			hage;
		int			hlv;
		double 	hhp;
		int			hhpmax;
		int		 	hap;
		int			hdp;
		int 		numbat;
		int			numsincelvl;
	
		//Enemy
		string	ename;
		int			elv;
		double	ehp;
		int			ehpmax;
		int			eap;
		int			edp;	
		
		//System
		bool		playing =		true;
		bool		ifplot	=		true;
		
		void	title();
		void	story(string hname);
		void	quit();
  	void	load();

		void	config			(string &hname,int &hage,int &hlv,double &hhp,int &hhpmax,int &hap,int &hdp,int &numbat,int &numsincelvl);	
		void	save				(string hname,int hage,int hlv,double hhp,int hhpmax,int hap,int hdp,int numbat,int numsincelvl);	
		void	herostats		(string hname,int hlv,double hhp,int hhpmax,int hap,int hdp,int numsincelvl);	
		void	enemystats	(string ename,double ehp,int ehpmax,int eap,int edp);	
		void	attack			(string user,string recev,double &HP,int AP,int DP);
		void	heal				(string user,string recev,double &HP,int HPMAX,int AP);
		void	level				(int &hlv,double &hhp,int &hhpmax,int &hap,int &hdp,int &numbat,int &numsincelvl);	
		void	mainmenu		(string hname,double &hhp,int &hhpmax,int &hap,int &hdp,int &numbat,int &numsincelvl,string ename,double &ehp,int eap,int edp);
		void	battle			(string hname,double &hhp,int &hhpmax,int &hap,int &hdp,string ename,double ehp,int ehpmax,int eap,int edp);
		void	enemy				(int hlv,string &ename,int &elv,double &ehp,int &ehpmax,int &eap,int &edp);

		int 	main()
{	
	title();
	
	while (playing == true)	
	{	
		mainmenu		(hname,hhp,hhpmax,hap,hdp,numbat,numsincelvl,ename,ehp,eap,edp);
	}
	
	quit();
	
	return 0;
}

		void	title()
{
	system("clear");
	cout	<< " _____   ________  \n";
	cout	<< "/\\  __`\\/\\_____  \\  \n";
	cout	<< "\\ \\ \\/\\ \\/____//'/'   \n";
	cout	<< " \\ \\ \\ \\ \\   //'/'  \n";
	cout	<< "  \\ \\ \\_\\ \\ //'/'___\n";
	cout	<< "   \\ \\_____\\/\\_______\\\n";
	cout	<< "    \\/_____/\\/_______/\n";		
	
	cout	<< "\n\t1. New Game\n";
	cout	<< "\t2. Load\n";
	cout	<< "\t3. Quit\n";
	cout	<< "\tAction: ";
	int choice;
	cin		>>	choice;
	if(choice == 1)
	{
		cout	<< "New Game...\n";
		config			(hname,hage,hlv,hhp,hhpmax,hap,hdp,numbat,numsincelvl);	
		system("clear");
		cout  << "Welcome to OZ\n";
		sleep(.5);
		system("clear");
		story(hname);
	}
	else if(choice == 2)
	{
		load();
	}
	else if(choice == 3)
	{
		quit();
	} 
}

		void	story(string hname)
{
	if(ifplot == true)
	{
//Scarecrow's Note
		system("clear");
		cout		<< 	"A Scarecrow appeared in your yard.\n You go to look at it.\n\n";
		cout		<<	hname	<<	" we need your help.\n";
		cout		<< 	"You need to come back to Oz.\n";
		cout		<<	"\t\t-The Scarecrow\n";
		sleep(8);
		system("clear");
	}
}

		void	quit()
{
			cout	<< "Are you sure?(Y/N): ";
			char quit;
			cin		>> quit;
			if(quit == 'Y')
			{
				cout <<	"Quitting...\n";
				save				(hname,hage,hlv,hhp,hhpmax,hap,hdp,numbat,numsincelvl);
				exit(0);
			} 
			else if(quit == 'y')
			{
				cout <<	"Quitting...\n";
				save				(hname,hage,hlv,hhp,hhpmax,hap,hdp,numbat,numsincelvl);
				exit(0);
			} 
			else
			{
				system("clear");
				cout << "Keep at it!\n";
			}
}

		void	load()
{
	cout <<	"Loading...";
}

		void	config			(string &hname,int &hage,int &hlv,double &hhp,int &hhpmax,int &hap,int &hdp,int &numbat,int &numsincelvl)
{
	cout 	<< "\nEnter Your Name: ";
	cin		>>	hname;

	cout	<< "Enter Your Age: ";
	cin		>>	hage;
	
	hlv =		 	1;
	hhp =		 			10;
	hhpmax =			10;
	hap =		 			10;
	hdp	=					10;
	numbat =		 	0;
	numsincelvl = 0;
	
	system("clear");

	cout << "You've Chosen:\n";
		cout	<< "\t\tName: \t\t\t" 					<< hname		<< "\n";
		cout	<< "\t\tAge: \t\t\t"						<< hage			<< "\n";
		cout	<< "\t\tHit Points(HP): \t" 		<< hhp			<< " / "	<< hhpmax	<< "\n";
		cout 	<< "\t\tAttack Points(AP): \t" 	<< hap 			<< "\n";
		cout	<< "\t\tDefense Points(DP): \t" << hdp			<< "\n";
	sleep(3);
}

		void	save				(string hname,int hage,int hlv,double hhp,int hhpmax,int hap,int hdp,int numbat,int numsincelvl)
{
	ofstream config;
	config.open ("townsend.conf");
		config	<< hname		<< "\n";
		config	<< hage			<< "\n";
		config	<< hlv		<< "\n";
		config	<< hhp			<< "\n";
		config	<< hhpmax		<< "\n";
		config	<< hap			<< "\n";
		config	<< numbat		<< "\n";
  config.close();
}

		void	herostats		(string hname,int hlv,double hhp,int hhpmax,int hap,int hdp,int numsincelvl)
{
	cout	<< "\t\t" << hname << "'s Stats" 		<< "(Level "		<< hlv	<< ")\n";
	cout	<< "\t\tHit Points(HP): \t" 				<< hhp					<< " / "	<< hhpmax	<< "\n";
	cout	<< "\t\tAttack Points(AP): \t" 			<< hap 					<< "\n";
	cout	<< "\t\tDefense Points(DP): \t" 		<< hdp					<< "\n";
	cout	<< "\t\tBattles since level up: " 	<< numsincelvl	<< "\n";
}


		void	enemystats	(string ename,double ehp,int ehpmax,int eap,int edp)
{
	cout	<< "\t\t" << ename << "'s Stats" 		<< "(Level "		<< elv	<< ")\n";
	cout	<< "\t\tHit Points(HP): \t" 				<< ehp					<< "\n";
	cout	<< "\t\tAttack Points(AP): \t"			<< eap					<< "\n";
	cout	<< "\t\tDefense Points(DP): \t" 		<< edp					<< "\n";
}

		void	attack			(string user,string recev,double &HP,int AP,int DP)
{
	cout	<< user << " has attacked "	<< recev << "!\n";
	HP = (HP - ((AP*.10)-(DP*.10)));
}

		void	heal				(string user,string recev,double &HP,int HPMAX,int AP)
{
	if(user == recev)
	{
	cout	<< user << " was healed" << "!\n";
	}
	if(user != recev)
	{
	cout	<< user << " healed "	<< recev << "!\n";
	}

	HP =	(HP + (AP*.10));
	if 		(HP	> HPMAX) HP = HPMAX;
}

		void	level				(int &hlv,double &hhp,int &hhpmax,int &hap,int &hdp,int &numbat,int &numsincelvl)
{
	if((numsincelvl/10) >= 1)
	{
			cout		<< "You Leveled Up!";
			hlv	= hlv++;
			hhpmax	=	hhpmax + hlv;
			hhp			= hhpmax;
			hap			= hap + hlv;
			hdp			=	hdp + hlv;
			herostats		(hname,hlv,hhp,hhpmax,hap,hdp,numsincelvl);		
			cout	<<	"\n";
		//Reset numsincelvl
		numsincelvl = 0;
	}
}

		void	mainmenu		(string hname,double &hhp,int &hhpmax,int &hap,int &hdp,int &numbat,int &numsincelvl,string ename,double &ehp,int eap,int edp)
{
	cout 	<< "Menu:\n";
		cout	<< "1. Go Adventure\n";
		cout	<< "2. Heal\n";
		cout	<< "3. Stats\n";
		cout	<< "4. Quit\n";
		cout	<< "Action: ";
		int maction;
		cin	>> maction;
		if 			(maction == 1)
		{
			system("clear");
			cout	<< "Looking for an enemy...\n";
			sleep(3);
			enemy				(hlv,ename,elv,ehp,ehpmax,eap,edp);
			sleep(1);
			cout	<< "Found an Enemy! Get Ready to battle!\n"	;
			battle			(hname,hhp,hhpmax,hap,hdp,ename,ehp,ehpmax,eap,edp);
			numbat = numbat++;
			numsincelvl = numsincelvl++;
		}
		else if (maction == 2)
		{
			system("clear");
			heal				(hname,hname,hhp,hhpmax,hap);
			herostats		(hname,hlv,hhp,hhpmax,hap,hdp,numsincelvl);	
		}
		else if (maction == 3)
		{	
			system("clear");
			herostats		(hname,hlv,hhp,hhpmax,hap,hdp,numsincelvl);		
			cout << "\t\tBattles Fought: \t"<< numbat	<< "\n";
		}	
		else if  (maction == 4) quit();
}

		void	battle			(string hname,double &hhp,int &hhpmax,int &hap,int &hdp,string ename,double ehp,int ehpmax,int eap,int edp)
{
	level				(hlv,hhp,hhpmax,hap,hdp,numbat,numsincelvl);

	cout	<< "Here comes an Enemy!\n";
	enemystats	(ename,ehp,ehpmax,eap,edp);
	while (ehp > 0) 
	{
		cout 	<< "Battle Menu:\n";
		cout	<< "1. Attack\n";
		cout	<< "2. Heal\n";
		cout	<< "3. Stats\n";
		cout	<< "Action: ";
		int maction;
		cin	>> maction;
		if 			(maction == 1)
			{
				system("clear");
		//Hero Attacks
				attack			(hname,ename,ehp,hap,edp);
				enemystats	(ename,ehp,ehpmax,eap,edp);
		//Enemy Attacks!!				
				attack			(ename,hname,hhp,eap,hdp);
				herostats		(hname,hlv,hhp,hhpmax,hap,hdp,numsincelvl);
			}
		else if (maction == 2)
			{
				system("clear");
				heal				(hname,hname,hhp,hhpmax,hap);
				herostats		(hname,hlv,hhp,hhpmax,hap,hdp,numsincelvl);
			}
		else if (maction == 3)
			{	
				system("clear");
				herostats		(hname,hlv,hhp,hhpmax,hap,hdp,numsincelvl);
				cout << "\t\tBattles Fought: \t"<< numbat	<< "\n";
			}	
	}
		system("clear");
		cout << "You Win!\n";
}

		void	enemy				(int hlv,string &ename,int &elv,double &ehp,int &ehpmax,int &eap,int &edp)
{
	if(hlv	<	3)
	{
		ename		=	"Sewer Rat";
		elv	=	1;
		ehp			=	5;
		ehpmax	=	5;
		eap			=	5;
		edp			=	5;
	}
		if(hlv	>=	3)
	{
		ename		=	"Big Rat";
		elv	=	1;
		ehp			= 10;
		ehpmax	=	10;
		eap			=	5;
		edp			=	5;
	}
}